package com.yoomiito.app.base;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.yoomiito.app.Constant;
import com.yoomiito.app.model.bean.UserInfo;
import com.yoomiito.app.model.config.ConfigInfo;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.utils.Event;
import com.yoomiito.app.utils.NetworkUtils;

import java.io.IOException;

import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.XApi;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.subjects.BehaviorSubject;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class BaseActPresenter<T extends BaseActivity> extends BasePresenter<T> {
    protected BehaviorSubject<Event> lifeSubject = BehaviorSubject.create();

    public BaseActPresenter(AppConfig appConfig) {
        super(appConfig);
    }


    public void getUserInfo() {
        YmApi.getYmService()
                .getUserInfo()
                .compose(getV().bindToLifecycle())
                .compose(XApi.<UserInfo>getApiTransformerData())
                .compose(XApi.<UserInfo>getScheduler())
                .subscribe(new ApiSubscriber<UserInfo>(handleException()) {
                    @Override
                    public void onSuccess(UserInfo userInfo) {
                        getV().setUserInfo(userInfo);
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

    protected <T> ObservableTransformer<T, T> bindLife() {
        return upstream -> (ObservableSource<T>) lifeSubject.takeUntil(lifeSubject.skipWhile(event -> event != Event.DESTROY && event != Event.DETACH));
    }

    public void getConfig(){
//        YmApi.getYmService()
//                .getConfigInfo()
//                .compose(getV().bindToLifecycle())
//                .compose(XApi.getApiTransformerData())
//                .compose(XApi.getScheduler())
//                .subscribe(new ApiSubscriber<ConfigInfo>(handleException()) {
//                    @Override
//                    public void onSuccess(ConfigInfo configInfo) {
//                        App.mAppConfig.mConfigInfo = configInfo;
//                        getV().dismissLoading();
//                        String oldConfig = SharedPref.getInstance(App.getInstance()).getString(Constant.Config,"");
//                        if (!TextUtils.isEmpty(oldConfig)){
//                            ConfigInfo oldConfigInfo = JSON.parseObject(oldConfig,ConfigInfo.class);
//                            if (oldConfigInfo!=null){
//                                String oldAddress = SharedPref.getInstance(App.getInstance()).getString(Constant.AddressInfo,"");
//                                if(TextUtils.isEmpty(oldAddress)||!oldConfigInfo.getAddress().getMsg().equals(configInfo.getAddress().getMsg())){//地址msg不一样时  缓存地址信息
//                                    saveAddress(configInfo.getAddress().getValue());
//                                }
//                            }
//                        }else {
//                            saveAddress(configInfo.getAddress().getValue());
//                        }
//                        String configStr = JSON.toJSONString(configInfo);
//                        SharedPref.getInstance(App.getInstance()).putString(Constant.Config,configStr);//每次都缓存配置信息
//                    }
//
//                    @Override
//                    public void onFail(ApiException error) {
//                        getV().dismissLoading();
//                    }
//                });
    }

    public void saveAddress(String str){
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(str)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                SharedPref.getInstance(App.getInstance()).putString(Constant.AddressInfo,data);
            }
        });
    }

}
